package sz.szsmk.citizencard.mvp.presenter;

import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.CollecAddReq;
import sz.szsmk.citizencard.model.network.bean.request.CollecQueryReq;
import sz.szsmk.citizencard.model.network.bean.request.WebsiteInfoReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CollecQueryRes;
import sz.szsmk.citizencard.model.network.bean.response.WebsiteInfoRes;
import sz.szsmk.citizencard.mvp.contract.IServiceDetailContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.ui.activity.IMainActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/12 20:03
 * description:
 */
public class ServiceDetailPresenter extends IBasePresenter
        implements IServiceDetailContact.IServiceDetailPresenter<IServiceDetailContact.IServiceDetailView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public ServiceDetailPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void websiteInfo(String longitude, String latitude, String id) {
        WebsiteInfoReq infoReq = new WebsiteInfoReq();
        infoReq.setLatitude(latitude);
        infoReq.setLongitude(longitude);
        infoReq.setWebsite_id(id);
        mNetworkDateSource.websiteInfo(infoReq, new SubscribeResult<WebsiteInfoRes>() {
            @Override
            protected void onOk(WebsiteInfoRes response) {
                if (response != null) {
                    String json = new Gson().toJson(response);
                    L.e(Constant.TAG, "服务网点详情---->" + json);
                    String picurl = response.getPicUrl();
                    ((IServiceDetailContact.IServiceDetailView) mView).websiteInfoSucc(response);
                }
            }

            @Override
            protected void onFailure(WebsiteInfoRes response) {
                super.onFailure(response);
                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            protected void onNext(WebsiteInfoRes response) {
                super.onNext(response);

                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

                Toast.makeText(DaggerApplication.getIns(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void addCollection(int officeId, String access_token) {
        CollecAddReq collecAddReq = new CollecAddReq();
        collecAddReq.setAccess_token(access_token);
        collecAddReq.setServiceOfficeId(officeId);
        mNetworkDateSource.addCollection(collecAddReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IServiceDetailContact.IServiceDetailView) mView).addSuccess(response.getMessage());
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IServiceDetailContact.IServiceDetailView) mView).addFail(response.getMessage());
            }

            @Override
            protected void onNext(BaseBean response) {
                super.onNext(response);
                ((IServiceDetailContact.IServiceDetailView) mView).addFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IServiceDetailContact.IServiceDetailView) mView).addFail(throwable.getMessage());

            }
        });

    }

    @Override
    public void cancelCollec(int officeId, String access_token) {
        CollecAddReq collecAddReq = new CollecAddReq();
        collecAddReq.setAccess_token(access_token);
        collecAddReq.setServiceOfficeId(officeId);
        mNetworkDateSource.deleteCollection(collecAddReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IServiceDetailContact.IServiceDetailView) mView).canSuccess(response.getMessage());
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IServiceDetailContact.IServiceDetailView) mView).canFail(response.getMessage());
            }

            @Override
            protected void onNext(BaseBean response) {
                super.onNext(response);
                ((IServiceDetailContact.IServiceDetailView) mView).canFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IServiceDetailContact.IServiceDetailView) mView).canFail(throwable.getMessage());

            }
        });
    }

    @Override
    public void queryCollect() {
        CollecQueryReq tokenRequest = new CollecQueryReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        tokenRequest.setAccess_token(access_token);
        String json = new Gson().toJson(tokenRequest);
        L.e(Constant.TAG, "tokenRequest---->" + json);
        mNetworkDateSource.queryCollect(tokenRequest, new SubscribeResult<CollecQueryRes>() {
            @Override
            protected void onOk(CollecQueryRes response) {
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "CollecQueryRes---->" + toJson);
                List<CollecQueryRes.DataBean> data = response.getData();
                if (data != null) {
                    ((IServiceDetailContact.IServiceDetailView) mView).queryCollectSucc(data);
                }
            }

            @Override
            protected void onNext(CollecQueryRes response) {
                super.onNext(response);
                L.e(Constant.TAG, "queryCollect" + response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
